<template>
  <el-dialog
    title="重新选择标准"
    :visible.sync="visible"
    :before-close="close"
    append-to-body
    :close-on-click-modal="false"
    width="90%">
    <el-row>
      <div style="color: red">
        <div>
          说明：
        </div>
        左侧为当前标准，右侧为候选标准（可选择，选择项有当前货品绑定过的老标准），对应项目一栏，是指将当前行的项目修改为右侧的哪一个项目，系统默认按名字来匹配，如对应错误请自行修改，如为空，那么右侧没有名字与之相匹配，请自行选择，如果是多余的项目，那么可以无视，之后在查看项目处删除此项目即可，如果当前标准不应该生效，那么请在标准里面修改当前标准为重新等待生效状态
      </div>
    </el-row>
    <el-divider></el-divider>
    <el-row>
      <el-col :span="12">
        <el-row>
          <div style="height: 40px;">
            当前标准：
            {{ nowStandard.insideInspectionStandardNum + (isCp ? ('('+nowStandard.nationInspectionStandardNum+')') : '') }}
          </div>
        </el-row>
        <el-row>
          <el-table
            :data="nowItems"
            border
            fit
            highlight-current-row
            style="width: 100%">
            <el-table-column
              prop="inspectionCategoryName"
              header-align="center"
              align="center"
              label="分类"
              width="50">
            </el-table-column>
            <el-table-column
              prop="inspectionItemName"
              header-align="center"
              align="center"
              label="项目名"
              width="150">
            </el-table-column>
            <el-table-column
              prop="printItemName"
              header-align="center"
              align="center"
              label="打印名"
              width="150">
            </el-table-column>
            <el-table-column
              v-if="!isCp"
              prop="insideInspectionItemValue"
              header-align="center"
              align="center"
              label="内控标准"
              width="200">
              <template slot-scope="{row}">
                <span v-html="row.insideInspectionItemValue"></span>
              </template>
            </el-table-column>
            <el-table-column
              v-else
              prop="nationInspectionItemValue"
              header-align="center"
              align="center"
              label="国家标准"
              width="200">
              <template slot-scope="{row}">
                <span v-html="row.nationInspectionItemValue"></span>
              </template>
            </el-table-column>
            <el-table-column
              prop="ci"
              header-align="center"
              align="center"
              label="对应项目">
              <template slot-scope="scope">
                <el-select v-model="scope.row.ci" value-key="inspectionItemId" filterable @change="changeItem(scope.$index)" placeholder="右侧编号" style="width: 100%;" size="mini">
                  <el-option
                    v-for="item in candidateStandard.items"
                    :key="item.inspectionItemId"
                    :label="item.inspectionItemName + '(' + item.inspectionItemId + ')'"
                    :value="item">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-col>
      <el-col :offset="1" :span="11">
        <el-row>
          <div style="height: 40px;">
            改为标准：
            <el-select v-model="candidateStandard" value-key="insideInspectionStandardId" @change="changeStandard(candidateStandard)" placeholder="选择标准" style="width: 70%" size="mini">
              <el-option
                v-for="s in candidateStandards"
                :key="s.insideInspectionStandardId"
                :label="s.insideInspectionStandardNum + (isCp ? ('('+s.nationInspectionStandardNum+')') : '')"
                :value="s">
              </el-option>
            </el-select>
          </div>
        </el-row>
        <el-row>
          <el-table
            :data="candidateStandard.items"
            border
            fit
            highlight-current-row
            style="width: 100%">
            <el-table-column
              prop="inspectionItemName"
              header-align="center"
              align="center"
              label="项目名"
              width="150">
            </el-table-column>
            <el-table-column
              prop="inspectionItemId"
              header-align="center"
              align="center"
              label="编号"
              width="70">
            </el-table-column>
            <el-table-column
              prop="inspectionCategoryName"
              header-align="center"
              align="center"
              label="分类">
            </el-table-column>
            <el-table-column
              prop="printItemName"
              header-align="center"
              align="center"
              label="打印名">
            </el-table-column>
            <el-table-column
              v-if="!c(childDataForm.checkTypeId)"
              prop="insideInspectionItemValue"
              header-align="center"
              align="center"
              label="内控标准"
              width="200">
              <template slot-scope="{row}">
                <span v-html="row.insideInspectionItemValue"></span>
              </template>
            </el-table-column>
            <el-table-column
              v-else
              prop="nationInspectionItemValue"
              header-align="center"
              align="center"
              label="国家标准">
              <template slot-scope="{row}">
                <span v-html="row.nationInspectionItemValue"></span>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button class="pan-btn primary-btn" type="primary" @click="submit">确 定</el-button>
  	</span>
  </el-dialog>
</template>

<script>
  import { isBlank } from '@/common/js/utils'
  import { CODE_OK } from '@/common/js/config'
  import { getChangeStandardInfoByMergeId,changeStandard } from '@/api/check'
  export default {
    data() {
    	return {
    		visible: false,
        isCp: false,
        nowStandard: {},
        nowItems: [],
        candidateStandards: [],
        candidateStandard: {}
    	}
    },
    props: ["childDataForm"],
    created() {
      if (!isBlank(this.childDataForm)) {
        this.isCp = this.c(this.childDataForm.checkTypeId)
        this.getInfo()
        this.visible = true
      }
    },
    methods: {
      getInfo() {
        const params = {
          checkMergeId: this.childDataForm.checkMergeId,
          productId: this.childDataForm.productId
        }
        getChangeStandardInfoByMergeId(params).then(res => {
          if (res.code === CODE_OK) {
            if (isBlank(res.candidateStandards)) {
              this.$message.error('此标准没有相关联的父级标准并且此货品当前没有关联任何标准，请检查')
              this.close()
            }else{
              this.nowStandard = res.nowStandard
              // 增加候选的属性
              this.$set(this.nowStandard,'cs',null)
              this.nowItems = res.nowItems.map(v => {
                this.$set(v,'ci',null)
                return v
              })
              this.candidateStandards = res.candidateStandards
              this.changeStandard(this.candidateStandards[this.candidateStandards.length-1])
            }
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      changeStandard(st) {
        // 1，如果选择的标准为当前标准的父级标准，那么直接对应item_id，
        // 2，否则，用检验项名来对应
        this.candidateStandard = st
        if (this.nowStandard.parentId == st.insideInspectionStandardId) {
          this.initItem('byId')
        }else{
          this.initItem('byName')
        }
      },
      initItem(opt) {
        let isById = opt == 'byId'
        this.nowStandard.cs = JSON.parse(JSON.stringify(this.candidateStandard))
        let cis = this.nowStandard.cs.items
        this.nowItems = this.nowItems.map(v => {
          v.ci = cis.find(ci => isById ? (ci.inspectionItemId == v.parentId) : (ci.inspectionItemName == v.inspectionItemName))
          return v
        })
      },
      changeItem(index) {
        //
      },
      close() {
      	this.$emit('close')
      	this.visible = false
      },
      submit() {
        let data
        if (!isBlank(data = this.formVerifyAndReturnData())) {
          changeStandard(data).then(res => {
            if (res.code === CODE_OK) {
              this.$message.success('修改成功')
            }else{
              this.$message.error(res.msg)
            }
            this.close()
          })
        }
      },
      formVerifyAndReturnData() {
        let st = JSON.parse(JSON.stringify(this.nowStandard))
        if (isBlank(st.cs)) {
          this.$message.error('请选择要修改成的目标标准')
          return null
        }
        // 修改standard
        st.checkMergeId = this.childDataForm.checkMergeId
        st.insideInspectionStandardId = st.cs.insideInspectionStandardId
        st.accordingToInside = st.cs.accordingToInside
        st.accordingToNation = st.cs.accordingToNation
        st.insideInspectionStandardNum = st.cs.insideInspectionStandardNum
        st.nationInspectionStandardNum = st.cs.nationInspectionStandardNum
        st.specialValue = st.cs.specialValue
        // 修改item
        let is = JSON.parse(JSON.stringify(this.nowItems))
        is = is.map(i => {
          if (!isBlank(i.ci)) {
            i.inspectionItemId = i.ci.inspectionItemId
            i.parentId = i.ci.parentId
            i.inspectionGroupId = i.ci.inspectionGroupId
            i.inspectionGroupName = i.ci.inspectionGroupName
            i.inspectionCategoryId = i.ci.inspectionCategoryId
            i.inspectionCategoryName = i.ci.inspectionCategoryName
            i.inspectionItemName = i.ci.inspectionItemName
            i.printItemName = i.ci.printItemName
            i.insideInspectionItemValue = i.ci.insideInspectionItemValue
            i.haveFormulaValue = i.ci.haveFormulaValue
            i.isNumber = i.ci.isNumber
            i.isHalfYearTest = i.ci.isHalfYearTest
            i.insideInspectionItemUpValue = i.ci.insideInspectionItemUpValue
            i.insideInspectionItemDownValue = i.ci.insideInspectionItemDownValue
            i.insideInspectionItemValueUnit = i.ci.insideInspectionItemValueUnit
            i.nationInspectionItemValue = i.ci.nationInspectionItemValue
            i.printValue = i.ci.printValue
            i.isPrint = i.ci.isPrint
            i.printConfig = i.ci.printConfig
            i.manHourOne = i.ci.manHourOne
            i.manHourTwo = i.ci.manHourTwo
            i.groupNum = i.ci.groupNum
            i.isHalf = i.ci.isHalf
            i.waterQuantityId = i.ci.waterQuantityId
            i.waterQuantityName = i.ci.waterQuantityName
            i.orderId = i.ci.orderId
          }
          return i
        })
        st.items = is
        return st
      }
    }
  }
</script>

<style>
</style>
